var QQMapWX = require('./qqmap-wx-jssdk.js');
var qqmapsdk = new QQMapWX({
    key: 'RKOBZ-HTLCD-3RX4S-HH5NW-SOE5Q-4ZBIG'
});

function getLocationAddress() {
    return new Promise((resolve, reject) => qqmapsdk.reverseGeocoder({
        // location: this.latitude + ',' + this.longitude, //获取表单传入的位置坐标,不填默认当前位置,示例为string格式
        get_poi: 1, //是否返回周边POI列表：1.返回；0不返回(默认),非必须参数
        success: function (res) { //成功后的回调
            let location = {};
            if (res.result) {
                location.latitude = res.result.location.lat;
                location.longitude = res.result.location.lng;
                location.address = res.result.address;
                location.district = res.result.ad_info.district;
                location.city = res.result.ad_info.city;
                location.province = res.result.ad_info.province;
                if (res.result.poi_count > 0) {
                    location.title = res.result.pois[0].title;
                }
            }
            resolve(location)
        },
        fail: function (error) {
            console.error(error);
            reject(error)
        },
        complete: function (res) {
            console.log(res);
        }
    }))
}

function reverseGeocoder(latitude, longitude) {
    return new Promise((resolve, reject) => qqmapsdk.reverseGeocoder({
        location: latitude + ',' + longitude, //获取表单传入的位置坐标,不填默认当前位置,示例为string格式
        success: function (res) { //成功后的回调
            let location = {};
            if (res.result.address_component) {
                location = res.result.address_component;
            }
            resolve(location)
        },
        fail: function (error) {
            console.error(error);
            reject(error)
        },
        complete: function (res) {
            console.log(res);
        }
    }))
}

module.exports = {
    getLocationAddress,
    reverseGeocoder
}